package com.yingxin.yxerp.module.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.helper.DateHelper;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.commons.vo.MessageInformation;
import com.yingxin.yxerp.module.dingtalk.service.ImgService;
import com.yingxin.yxerp.module.dingtalk.service.MessageNotificationService;
import com.yingxin.yxerp.module.report.entity.TaskExecuteErrorDaily;
import com.yingxin.yxerp.module.report.entity.TaskExecuteErrorDailyItem;
import com.yingxin.yxerp.module.report.entity.enums.DynamicTaskLogStatusEnums;
import com.yingxin.yxerp.module.report.mapper.TaskExecuteErrorDailyMapper;
import com.yingxin.yxerp.module.report.service.IDynamicTaskLogService;
import com.yingxin.yxerp.module.report.service.ITaskExecuteErrorDailyItemService;
import com.yingxin.yxerp.module.report.service.ITaskExecuteErrorDailyService;
import com.yingxin.yxerp.module.report.vo.req.DynamicTaskLogQueryVo;
import com.yingxin.yxerp.module.report.vo.resp.DynamicTaskLogRespVo;
import com.yingxin.yxerp.module.sys.entity.enums.DingChatType;
import com.yingxin.yxerp.module.sys.service.IBusinessTypeSettingService;
import org.activiti.engine.impl.util.CollectionUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 任务执行异常提醒 - 日报表 - 实现类
 * @author yuebao
 * @date 2023-12-13
 */

@Service
public class TaskExecuteErrorDailyServiceImpl extends ServiceImpl<TaskExecuteErrorDailyMapper, TaskExecuteErrorDaily>
        implements ITaskExecuteErrorDailyService {

    @Autowired
    private IDynamicTaskLogService taskLogService;

    @Autowired
    private ITaskExecuteErrorDailyItemService itemService;

    @Autowired
    private IBusinessTypeSettingService businessTypeSettingService;

    @Autowired
    private ImgService imgService;

    @Autowired
    private MessageNotificationService messageNotificationService;

    /**
     * 获取 mapper
     */
    @Override
    public TaskExecuteErrorDailyMapper mapper(){
        return baseMapper;
    }

    /**
     * 获取任务执行异常的数据发送钉钉系统管理部
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String execute(){
        DynamicTaskLogQueryVo queryVo = new DynamicTaskLogQueryVo();
        queryVo.setStatus( DynamicTaskLogStatusEnums.FAIL.getStatus().toString() );
        queryVo.setCreateTimeBegin( DateHelper.getYesterDay0849() );
        queryVo.setCreateTimeEnd( DateHelper.getToDay0850() );

        List<DynamicTaskLogRespVo> list = taskLogService.mapper().listVo( queryVo );
        if( CollectionUtil.isEmpty( list ) ){
            return MessageInformation.NO_DATA;
        }

        TaskExecuteErrorDaily daily = new TaskExecuteErrorDaily();
        daily.setId( IdHelper.snowflakeId() );
        daily.setReportNo( businessTypeSettingService.next( BusinessType.TEEDS ) );
        daily.setReportDate( DateHelper.getYesterDayyyyyMMdd() );
        daily.setRemarks(
                DateHelper.dateFormat( queryVo.getCreateTimeBegin(), "yyyy-MM-dd HH:mm:ss" ) +
                "至" +
                DateHelper.dateFormat( queryVo.getCreateTimeEnd(), "yyyy-MM-dd HH:mm:ss" ) );
        daily.setCreatorId( CommonConst.TRUE );
        daily.setUpdaterId( CommonConst.TRUE );
        daily.setDelFlag( CommonConst.FALSE );

        // 生成图片
        Map<String, Object> data = new HashedMap();
        data.put( "bill", daily );
        data.put( "items", list );

        List< TaskExecuteErrorDailyItem > items = new ArrayList<>();
        for( DynamicTaskLogRespVo vo : list ){
            TaskExecuteErrorDailyItem item = new TaskExecuteErrorDailyItem();
            item.setId( IdHelper.snowflakeId() );
            item.setBillId( daily.getId() );
            item.setTaskLogId( vo.getId() );
            item.setCreatorId( daily.getCreatorId() );
            item.setUpdaterId( daily.getUpdaterId() );
            item.setDelFlag(daily.getDelFlag() );

            items.add( item );
        }

        boolean bool = create( daily, items );

        String resultStr = imgService.htmlToImage(
                data, "TaskExecuteErrorDaily.ftl", 1200, 80 + 20 + list.size() * 20 );
        daily.setFilePath( resultStr );

        bool = updateById( daily );

        // 发送钉钉
        if ( StringUtils.isNotEmpty( resultStr ) & bool ){
            messageNotificationService.sendGroupImgMsg( resultStr, DingChatType.TEERBB.getType() );
        }

        return MessageInformation.SUCCESS_MEG;
    }

    /**
     * 创建
     */
    @Override
    public boolean create(TaskExecuteErrorDaily bill, List<TaskExecuteErrorDailyItem> items ){
        if( CollectionUtil.isEmpty( items ) ){
            return false;
        }
        save( bill );
        itemService.saveBatch( items );
        return true;
    }

}